package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.service.IdcMenuService;
import com.soft.idc.common.util.JQPage;

public class MenuManagerController extends Controller {
	public void index() {
		this.render("/menu/menu.html");
	}

	public void queryMenu() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		IdcUser u = this.getSessionAttr("SYS_USER");
		StringBuffer sb = new StringBuffer();
		String menuName = this.getPara("menu_name");
		String menuCode = this.getPara("menu_code");
		String url = this.getPara("url");
		String parentMenuName = this.getPara("parent_menu_name");
		
		sb.append("SELECT M.MENU_ID, M.PARENT_ID, ");
		sb.append("PM.NAME AS PARENT_NAME, ");
		sb.append("M.CODE, M.NAME, M.TYPE, M.URL, ");
		sb.append("M.SCRIPT, M.LOGO, M.SEQ, M.CSSCLASS ");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append(" FROM IDC_MENU M LEFT JOIN IDC_MENU PM ON PM.MENU_ID = M.PARENT_ID WHERE 1=1 ");
		if(menuName !=null ){
			sqlFrom.append(" AND M.NAME LIKE '%" + menuName + "%' ");
		}
		
		if(menuCode !=null ){
			sqlFrom.append(" and M.CODE like '%" + menuCode + "%' ");
		}
		
		if(url !=null ){
			sqlFrom.append(" and M.URL like '%" + url + "%' ");
		}
		if(parentMenuName !=null){
			sqlFrom.append(" and PM.NAME like '%" + parentMenuName + "%' ");
		}
		
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());
		JQPage jPage = new JQPage(page);
		System.out.println(JsonKit.toJson(jPage));
		this.renderJson(jPage);
	}

	public void add() {
		String code = this.getPara("code");
		String name = this.getPara("name");
		String parent_id = this.getPara("parent_id");
		String type = this.getPara("type");
		String url = this.getPara("url");
		String seq = this.getPara("seq");
		String cssClass = this.getPara("cssClass");
		String sql = "INSERT INTO IDC_MENU (CODE, NAME, PARENT_ID, TYPE, URL, SEQ, CSSCLASS) VALUES (? , ? , ?, ?, ?, ?, ?)";
		int  result = Db.update(sql, new Object[]{code, name, parent_id, type, url, seq, cssClass});
		this.renderJson(result);
	}

	public void delete() {
		final String menuIds = this.getPara("menuIds");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] roleIdArr = menuIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("DELETE FROM IDC_MENU WHERE MENU_ID IN(");
				for(int i=0; i<roleIdArr.length; i++){
					if(i==roleIdArr.length-1){
						sb.append(roleIdArr[i] + ")");
					}else{
						sb.append(roleIdArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==roleIdArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}

	public void edit() {
		String menu_id = this.getPara("menuId");
		String code = this.getPara("code");
		String name = this.getPara("name");
		String parent_id = this.getPara("parent_id");
		String type = this.getPara("type");
		String url = this.getPara("url");
		String seq = this.getPara("seq");
		String cssClass = this.getPara("cssClass");
		String sql = "UPDATE IDC_MENU SET CODE=?, NAME=?, PARENT_ID=?, TYPE=?, URL=?, SEQ=?, CSSCLASS=? WHERE MENU_ID =? ";
		int  result = Db.update(sql, new Object[]{code, name, parent_id, type, url, seq, cssClass, menu_id});
		this.renderJson(result);
	}
	
	public void queryMenuList(){
		List<Record> result= IdcMenuService.queryAllMenu();
		this.renderJson(result);
	}
	
	public void queryParentList(){
		List<Record> result= IdcMenuService.queryParentMenu();
		this.renderJson(result);
	}
}
